﻿<cfsilent>
	<cfscript>

		unitAdvice = getProperty("serviceFactory").getBean("unitAdvice");
		
		event.setArg("pageTitle", "学籍电子照片管理");
		
		currentTab = event.getArg("TabID", "labManager");
		
		passArgs = structNew();
		
		/* 当前在校生判断 grade + schooling_length > ? */
		gradeLeast = year( now() );
		if ( month( now() ) < 9 ) {
			gradeLeast = year( now() ) - 1;
		}
		
		/* 计算全校无照片的学生情况 */
		
		sql = "SELECT 
				d.institute_id, 
				d.institute_name, 
				c.sbj_id, 
				c.sbj_name, 
				b.grade, 
				count(*) stu_count 
				FROM t_student a 
					 INNER JOIN t_class b ON b.cls_id = a.cls_id 
					 INNER JOIN t_subject c ON c.sbj_id = b.sbj_id 
					 INNER JOIN t_institute d ON d.institute_id = c.institute_id 
					 INNER JOIN t_student_status e ON e.stu_id = a.stu_id 
				WHERE 
					SUBSTR(e.student_prop, 18, 1) = '0' 
					AND 
					b.grade + c.schooling_length > :yearNow 
				GROUP BY 
					d.institute_id, 
				  d.institute_name, 
				  c.sbj_id, 
				  c.sbj_name, 
				  b.grade
				ORDER BY 
					d.institute_name";
					
		queryObj = new Query( datasource=application.dnsSlave) ;
		queryObj.addParam( name="yearNow", value=gradeLeast , cfsqltype="cf_sql_decimal", scale =1 );
		rs_summary = queryObj.execute( sql=sql ).getResult();	

		gradeArray = [];
		instituteArray = [];
		subjectArray = [];
		
		gradeList = "";
		
		studentCount = 0;
		
		summarySet = structNew();
		
		for (a = 1; a lte rs_summary.recordCount; a++) {
			
			if ( not listFind( gradeList, rs_summary["grade"][a], "," ) ) {
				gradeList = listAppend( gradeList, rs_summary["grade"][a], "," );
			}
			
			if ( not arrayFind( instituteArray, [ rs_summary["institute_id"][a], rs_summary["institute_name"][a], [], 0 ] ) ) {
				arrayAppend( instituteArray, [ rs_summary["institute_id"][a], rs_summary["institute_name"][a], [], 0 ] );
			}
			
			if ( not arrayFind( subjectArray, [ rs_summary["sbj_id"][a], rs_summary["sbj_name"][a], rs_summary["institute_id"][a], 0 ] ) ) {
				arrayAppend( subjectArray, [ rs_summary["sbj_id"][a], rs_summary["sbj_name"][a], rs_summary["institute_id"][a], 0 ] );
			}
			
			structInsert( summarySet, "P-" & rs_summary["sbj_id"][a] & "-" & rs_summary["grade"][a], rs_summary["stu_count"][a] );
			
			studentCount += rs_summary["stu_count"][a];
			
		}
		
		for ( grade in listToArray( listSort(gradeList, "text", "asc"), "," ) ) {
			arrayAppend( gradeArray, [ grade, 0 ] );
		}

		for (a = 1; a lte rs_summary.recordCount; a++) {
			
			for ( i=1; i lte arrayLen(gradeArray); i++ ) {

				if ( gradeArray[i][1] eq rs_summary["grade"][a] ) {
					gradeArray[i][2] += rs_summary["stu_count"][a];
				}
				
			}
			
			for ( i=1; i lte arrayLen(subjectArray); i++ ) {

				if ( subjectArray[i][1] eq rs_summary["sbj_id"][a] ) {
					subjectArray[i][4] += rs_summary["stu_count"][a];
				}
				
			}
			
		}
		
		for ( subject in subjectArray ) {
			for ( i=1; i lte arrayLen(instituteArray); i++ ) {
				
				if ( instituteArray[i][1] eq subject[3] ) {
					arrayAppend( instituteArray[i][3], [ subject[1], subject[2], subject[4] ] );
					instituteArray[i][4] ++;
				}
				
			}
		}
		
		uploadDir = getProperty("cdnAssetsPath") & "/tmp/stuPicture/";

	</cfscript>
</cfsilent>

<div id="headArea">
	<div class="uiHeader uiHeaderWithImage ptm">
		<div class="clearfix uiHeaderTop">
			<div class="uiHeaderActions rfloat">
				<a class="uiButton" href="<cfoutput>#buildURL('DEFAULT_EVENT')#</cfoutput>"><i class="mrs img btnList"></i><span class="uiButtonText">学籍信息管理</span></a>
			</div>
			<div>
				<h2 class="uiHeaderTitle">
					<i class="uiHeaderImage img icon16x16 picture"></i>电子照片管理
				</h2>
			</div>
		</div>
	</div>
</div>

<div class="UIElement">
	<ul class="senateFirstPage">
		<li>
			<div class="clearfix" id="create">
				<div class="UItab">
					<div class="tabLables">
						<span <cfif currentTab eq "labManager">class="active"</cfif> id="labManager" tabTarget="Manager">照片管理</span>
						<span <cfif currentTab eq "labLack">class="active"</cfif> id="labLack" tabTarget="Lack">无照片学籍</span>
						<span <cfif currentTab eq "labImport">class="active"</cfif> id="labImport" tabTarget="Import">批量导入照片</span>
					</div>
				</div>
				<script language="javascript" type="text/javascript">
					//<![CDATA[
					<!--
					$(document).ready(function() {
						/* init Tab */
						$("div.tabContent").hide();
						$("div#" + $("div.tabLables span.active").attr("tabTarget")).show();
						
						$("div.tabLables > span").click(function(){
							var $targetID = $(this).attr("tabTarget");
							
							$("div.tabLables span").removeClass("active");
							$(this).addClass("active");
							
							$("div.tabContent").not('[id="' + $targetID + '"]').hide();
							$("div.tabContent#" + $targetID).show();
						});
					});
					
					// -->
					//]]>
				</script>
				
				<div id="Manager" class="tabContent">
					
					<div class="noticeBlock">
						<h3>照片资源管理</h3>
						<p><span class="img icon16x16 message"></span>照片包含未同步的临时文件和上传的打包文件, 您可以这里对文件进行集中管理</p>
						<hr/>
					</div>
					
					<!--- 临时文件统计 --->
					
					<cfset fileCount = 0 />
					<cfset fileSizes = 0 />

					<cfset jpgCount = 0 />
					<cfset jpgSizes = 0 />
					
					<cfset waitCount = 0 />
					<cfset waitSizes = 0 />
					
					<cfset picArray = [] />
					
					<cfset tmpDir = getProperty("cdnAssetsPath") & "/tmp/stuPicture/" />

					<cfdirectory name="tmpFiles" listinfo="all" action="list" recurse="no" filter="*.*" directory="#tmpDir#" />
					
					<cfloop query="tmpFiles">
					
						<cfset fileCount ++ />
						<cfset fileSizes += tmpFiles.size />
						
						<cfif lCase( listLast( tmpFiles.name, "." ) ) eq "jpg" >
							<cfset jpgCount ++ />
							<cfset jpgSizes += tmpFiles.size />

							<cfset arrayAppend( picArray, tmpFiles.name ) />
						</cfif>
						
					</cfloop>

					<cfif fileCount>
					
						<div class="noticeBlock">
							<div class="operation">
								<a class="uiButton" href="javascript:processPictures();"><i class="mrs img btnExecute"></i><span class="uiButtonText">同步学籍照片</span></a>
							</div>
							<h3>临时照片文件</h3>
						</div>

						<table class="UItable">
							<tr class="cellBorder">
								<td rowspan="2" align="center">临时文件</td>
								<td rowspan="2" align="center">存储空间</td>
								<td colspan="2" align="center">JPG 图片</td>
								<td colspan="2" align="center">其他</td>
							</tr>
							<tr class="cellBorder">
								<td align="center">文件数量</td>
								<td align="center">存储空间</td>
								<td align="center">文件数量</td>
								<td align="center">存储空间</td>
							</tr>
							<tr class="cellBorder">
								<td align="center"><span class="number"><cfoutput>#numberFormat( fileCount )#</cfoutput></span></td>
								<td align="center"><span class="number"><cfoutput>#unitAdvice.getFileSize( fileSizes )#</cfoutput></span></td>
								<td align="center"><span class="number"><cfoutput>#numberFormat( jpgCount )#</cfoutput></span></td>
								<td align="center"><span class="number"><cfoutput>#unitAdvice.getFileSize( jpgSizes )#</cfoutput></span></td>
								<td align="center"><span class="number"><cfoutput>#numberFormat( fileCount-jpgCount )#</cfoutput></span></td>
								<td align="center"><span class="number"><cfoutput>#unitAdvice.getFileSize( fileSizes-jpgSizes )#</cfoutput></span></td>
							</tr>
						</table>
						
						<div id="processPicturesBox" class="hidden_elem">
							
							<div class="noticeBlock">
								<h3>处理临时照片</h3>
								<p><span class="img icon16x16 message"></span>在处理队列完成之前, 请不要进行其他操作.</p>
							</div>
							<div class="UIProgressbar">
								<span class="progressBar" id="pb1">0%</span>
								<p id="output">处理队列初始化</p>
								<cfset structInsert( passArgs, "TabID", "labLack", true ) />
								<p id="picComplete">同步操作已完成, <a href="<cfoutput>#buildURL('studentPictureManager', passArgs)#</cfoutput>">点这里查看电子学籍照片缺失统计情况</a></p>
								<cfset structDelete( passArgs, "TabID", false ) />
							</div>
							<div class="noticeBlock">
								<ol id="errorlogs"></ol>
							</div>
							
						</div>

						<cfset cellPerRow = 20 />
						
						<!---<div id="out"></div>--->
						
						<script language="javascript" type="text/javascript">
							//<![CDATA[
							<!--
							
							<!--- 每 $cellPerRow$ 张照片一组 --->
							var picData = [
											<cfif arrayLen(picArray) gt cellPerRow >
												<cfloop from="1" to="#Ceiling( arrayLen(picArray) / cellPerRow )#" index="g">
													<cfif g lt Ceiling( arrayLen(picArray) / cellPerRow )>
														[ <cfloop from="#cellPerRow*(g-1)+1#" to="#cellPerRow*g#" index="c">'<cfoutput>#picArray[c]#</cfoutput>'<cfif c lt cellPerRow*g>, </cfif></cfloop> ], 
														<cfelse>
														[ <cfloop from="#cellPerRow*(g-1)+1#" to="#arrayLen(picArray)#" index="c">'<cfoutput>#picArray[c]#</cfoutput>'<cfif c lt arrayLen(picArray)>, </cfif></cfloop> ]
													</cfif>
												</cfloop>
												<cfelse>
												[ <cfloop from="1" to="#arrayLen(picArray)#" index="c">'<cfoutput>#picArray[c]#</cfoutput>'<cfif c lt arrayLen(picArray)>, </cfif></cfloop>  ]
											</cfif>
											];
							
							function processPictures() {
								
								/* 显示阻挡信息 */
								$.blockUI({ 
											message: '正在处理临时照片文件...',
											css: { 
													'width': '16%',
													'left': '42%',
													'border': 'none', 
													'padding': '15px 5px', 
													'backgroundColor': '#333', 
													'border-radius': '4px', 
													'opacity': .6, 
													'color': '#fff',
													'cursor': null
													},
											overlayCSS: {
													'backgroundColor': '#aaa', 
													'opacity': .1 ,
													'cursor': null
													}
											}); 
								
								/* init Progress */
								$("#pb1").progressBar({
									boxImage		: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbar.gif',
									barImage		: {
														0:  '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_red.gif',
														30: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_orange.gif',
														70: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_green.gif'
														}
									});
								
								$("#picComplete").addClass("hidden_elem");
								
								$("#processPicturesBox").removeClass("hidden_elem");
								
								$("#output").html("需要处理 <cfoutput>#arrayLen(picArray)#</cfoutput> 张临时图片文件, 将分为 <cfoutput>#Ceiling( arrayLen(picArray) / cellPerRow )#</cfoutput> 个批次进行");
								
								progressPictures(0);
								
								return void(0);
							}
							
							
							function progressPictures(current) {

								$.post(
									"<cfoutput>#buildURL('apiProcessPictures')#</cfoutput>",
									{
										'fileNames': picData[current].join("*"),
										'seed': (new Date()).getTime()
									},
									function(data){
										
										//$("#out").html(data);
										response = $.parseJSON(data);
										
										$("#output").html( "PROC-" + current + "/" + picData.length + ". " + response.operation);

										/*
										for ( var e=0; e < response.errorLog.length; e++ ) {
											$("<li></li>").html(response.errorLog[e]).appendTo( $("#errorlogs") );
										}
										*/
										
										/* fresh progress bar */
										var percentage = Math.floor(100 * parseInt(current + 1) / parseInt(picData.length));
										
										$("#pb1").progressBar(
																percentage,
																{
																	boxImage : '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbar.gif',
																	barImage : {
																		0:  '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_red.gif',
																		30: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_orange.gif',
																		70: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_green.gif'
																	},
																	callback: function(running){
																	
																		if (running.running_value == running.value) {
																	
																			current++;
											
																			if ( current < picData.length) {
																				progressPictures(current);
																			}
																			else {
																				
																				$("#output").text("完成");
																				$("#picComplete").removeClass("hidden_elem");
																				
																				/* 取消阻挡 */
																				$.unblockUI();
																			}
																		}
																	}
																	
																});
										
									});

								
							}
							
							// -->
							//]]>
						</script>
						
						<cfelse>
						
						<div class="systemNotice">
							<h3>上传临时文件</h3>
							<p class="mln">请按照 学号 对图片文件命名, 上传至服务器 <em><cfoutput>#uploadDir#</cfoutput></em> 目录</p>
							<ol>
								<li>命名： 电子照片必须按照学生的学号进行命名</li>
								<li>格式： 图片必须使用 jpg 格式进行保存, 文件最小分辨率为 480x640, 高宽比为 4:3</li>
							</ol>
						</div>
						
					</cfif>
				
				</div>
				
				<div id="Lack" class="tabContent">
					
					<div class="noticeBlock">
						<div class="operation">
							<a class="uiButton" href="javascript:processPicSign();"><i class="mrs img btnPublish"></i><span class="uiButtonText">刷新统计数据</span></a>
						</div>
						<h3>无照片学籍档案统计</h3>
					</div>
					
					<cfif studentCount>
						
						<div id="signPicturesBox" class="hidden_elem">
							<div class="UIProgressbar">
								<span class="progressBar" id="pb2">0%</span>
								<p id="outputSign">处理队列初始化</p>
							</div>
						</div>
						
						<!---<div id="out"></div>--->

						<table class="UItable">
							<tr class="cellBorder">
								<td>学院</td>
								<td>专业</td>
								<cfloop array="#gradeArray#" index="gradeSet">
									<td width="50" align="center"><span class="number"><cfoutput>#gradeSet[1]#</cfoutput></span></td>
								</cfloop>
								<td width="60" align="center">合计</td>
							</tr>
							
							<cfloop array="#instituteArray#" index="dep">
								<cfset shown = false />
								<cfloop array="#dep[3]#" index="subject">
									<tr class="cellBorder">
										<cfif not shown>
											<td rowspan="<cfoutput>#dep[4]#</cfoutput>"><cfoutput>#dep[2]#</cfoutput></td>
											<cfset shown = true />
										</cfif>
										<td><cfoutput>#subject[2]#</cfoutput></td>
										<cfloop array="#gradeArray#" index="gradeSet">
											<td align="center">
												<cfif structKeyExists( summarySet, "P-" & subject[1] & "-" & gradeSet[1] ) ><span id="P-<cfoutput>#subject[1]#</cfoutput>-<cfoutput>#gradeSet[1]#</cfoutput>" class="number"><cfoutput>#summarySet["P-" & subject[1] & "-" & gradeSet[1]]#</cfoutput></span></cfif>
											</td>
										</cfloop>
										<td align="center"><span class="number"><cfoutput>#subject[3]#</cfoutput></span></td>
									</tr>
								</cfloop>
							</cfloop>
							
							<tr class="cellBorder">
								<td colspan="2">合计</td>
								<cfloop array="#gradeArray#" index="gradeSet">
									<td width="50" align="center"><span class="number"><cfoutput>#gradeSet[2]#</cfoutput></span></td>
								</cfloop>
								<td align="center"><span class="number"><cfoutput>#studentCount#</cfoutput></span></td>
							</tr>
						</table>
						
						<cfset row = 0 />
						<script language="javascript" type="text/javascript">
							//<![CDATA[
							<!--
							
							var signData = [
											<cfloop query = "rs_summary" ><cfset row++ />['<cfoutput>#rs_summary.sbj_id#</cfoutput>', '<cfoutput>#rs_summary.sbj_name#</cfoutput>', '<cfoutput>#rs_summary.grade#</cfoutput>']<cfif row lt rs_summary.recordCount>,</cfif></cfloop>
											];
							
							function processPicSign() {
								
								/* 显示阻挡信息 */
								$.blockUI({ 
											message: '正在重新统计电子学籍照片情况...',
											css: { 
													'width': '16%',
													'left': '42%',
													'border': 'none', 
													'padding': '15px 5px', 
													'backgroundColor': '#333', 
													'border-radius': '4px', 
													'opacity': .6, 
													'color': '#fff',
													'cursor': null
													},
											overlayCSS: {
													'backgroundColor': '#aaa', 
													'opacity': .1 ,
													'cursor': null
													}
											}); 
								
								/* init Progress */
								$("#pb2").progressBar({
									boxImage		: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbar.gif',
									barImage		: {
														0:  '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_red.gif',
														30: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_orange.gif',
														70: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_green.gif'
														}
									});
								
								$("#signPicturesBox").removeClass("hidden_elem");
								
								progressPicturesSign(0);
								
								return void(0);
							}
							
							function progressPicturesSign(current) {
								
								$.post(
									"<cfoutput>#buildURL('apiProcessSign')#</cfoutput>",
									{
										'SubjectID': signData[current][0],
										'Grade': signData[current][2],
										'seed': (new Date()).getTime()
									},
									function(data){
										
										//$("#out").html(data);
										response = $.parseJSON(data);
										
										$("#outputSign").html( "PROC-" + current + "/" + signData.length + ". " + response.operation );
										
										$("#P-" + response.SubjectID + "-" + response.Grade ).addClass("alert").html( response.Missing );

										/* fresh progress bar */
										var percentage = Math.floor(100 * parseInt(current + 1) / parseInt(signData.length));
										
										$("#pb2").progressBar(
																percentage,
																{
																	boxImage : '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbar.gif',
																	barImage : {
																		0:  '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_red.gif',
																		30: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_orange.gif',
																		70: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_green.gif'
																	},
																	callback: function(running){
																	
																		if (running.running_value == running.value) {
																	
																			current++;
											
																			if ( current < signData.length) {
																				progressPicturesSign(current);
																			}
																			else {
																				
																				$("#signPicturesBox").addClass("hidden_elem");
																				
																				/* 取消阻挡 */
																				$.unblockUI();
																			}
																		}
																	}
																	
																});
										
									});

								
							}
										
							
							// -->
							//]]>
						</script>
						
					</cfif>
					
				</div>

				<div id="Import" class="tabContent">
					
					<div class="noticeBlock">
						<h3>批量导入电子照片</h3>
						<p><span class="img icon16x16 message"></span>请严格按照如下的规则对要上传的图片文件进行规范命名和处理, 并打包后上传.</p>
						<ol>
							<li>命名： 电子照片必须按照学生的学号进行命名</li>
							<li>格式： 图片必须使用 jpg 格式进行保存, 文件最小分辨率为 480x640, 高宽比为 4:3</li>
							<li>打包： 使用 zip 格式对多个图片进行打包, 打包后文件体积不要超过 100M, 以免上传文件超时或过长的解包时间</li>
						</ol>
						<hr/>
					</div>
					
					<form onSubmit="javascript:return processVerfiyForm('formImport');" id="formImport" class="formWrapper" method="post" enctype="multipart/form-data" action="<cfoutput>#buildURL('uploadPictureZipDo')#</cfoutput>">
						
						<input name="TabID" type="hidden" value="labImport" />
						
						<div class="label">
							<span class="req">*</span><b>导入图片包</b>
							<input name="upload" id="upload" type="file" verification="required" />
						</div>
						
						<div class="lable"><input id="importTrigger" class="button" type="submit" value="导入照片" /></div>
					</form>

				</div>
				
			</div>
		</li>
	</ul>
</div>
				